var pageObj = {
	key: location.href.substr(location.origin.length),
	pageMap: JSON.parse(sessionStorage['hxt-page'] || '{}')
};

function merge(a) {
	function t(cn) {
		if (localStorage.lang != 'en')
			return cn;
		return {
			请选择至少1个: 'Please select at least 1',
			删除成功: 'Deleted successfully',
			'请选择.xls或.xlsx文件': 'Please select .xls or .xlsx file',
			成功: 'succeed'
		}[cn] || '';
	}

	var o = {
		el: '#vue-root',
		data: {
			pageNum: pageObj.pageMap[pageObj.key] || 1,
			pageSize: 10,
			total: 0,
			pages: 0,
			all: false,
			list: [],
			bigimg: false,
			big_src: null,
			urls: {
				list: null,
				del: null
			},
			search: JSON.parse(sessionStorage['search-' + pageObj.key] || '{}'), // 恢复筛选条件
			able: {
				add: false,
				edit: false,
				del: false
			},
		},
		created: function () {
			this.getData();
		},
		methods: {
			resetSearch: function () {
				this.search = {}; // 清空筛选条件
				sessionStorage.removeItem('search-' + pageObj.key); // 清除本地缓存
				this.pageNum = 1; // 重置到第一页
				this.getData(); // 重新加载数据
			},
			getData: function () {
				axios.get(this.urls.list, {
					params: Object.assign({
						pageNum: this.pageNum,
						pageSize: this.pageSize
					}, this.search)
				}).then(this.setData).catch(function (err) {
					console.log(err);
				});
			},
			getData0: function () {
				if (this.pageNum > 1) this.pageNum = 1;
				else this.getData();
			},
			setData: function (res) {
				this.list = res.data.list;
				this.total = res.data.total;
				this.pages = res.data.pages;
				if (this.list.length == 0 && this.pageNum != 1)
					this.pageNum = 1;
			},
			batchDelete: function () {
				this.del(this.checkedIds);
			},
			del: function (ids, meal) {
				console.log(meal);
				if (ids.length === 0) {
					alert(t('请选择至少1个'));
					return;
				}
				if (!confirm(this.delMsg(ids))) return;
				axios.post(this.urls.del, ids).then(function (res) {
					alert(t('删除成功'));
					this.getData();
					if (meal) this.getMeal();
				}.bind(this));
			},
			delMsg: function (ids) {
				return localStorage.lang == 'en' ? 'confirm deletion?' : '确定删除这' + ids.length + '项吗？';
			},
			importExcel: function () {
				var path = this.$refs.file.value;
				if (!path.endsWith('.xls') && !path.endsWith('.xlsx')) {
					alert(t('请选择.xls或.xlsx文件'));
					return;
				}
				axios.post(this.urls.imports, new FormData(this.$refs.form)).then(this.imported).catch(function () {
					this.$refs.file.value = '';
				}.bind(this));
			},
			imported: function () {
				this.getData();
				alert(t('成功'));
				this.$refs.file.value = '';
			}
		},
		watch: {
			pageNum: function (newValue, oldValue) {
				this.getData();
				pageObj.pageMap[pageObj.key] = newValue;
				if (newValue == 1) delete pageObj.pageMap[pageObj.key];
				sessionStorage['hxt-page'] = JSON.stringify(pageObj.pageMap);
			},
			all: function (newValue, oldValue) {
				this.list.forEach(function (it) {
					it.checked = newValue;
				});
			},
			pageSize: function (newValue, oldValue) {
				this.getData();
			},
			search: {
				handler: function (newValue) {
					sessionStorage['search-' + pageObj.key] = JSON.stringify(newValue); // 保存筛选条件
				},
				deep: true
			}
		},
		computed: {
			pageList: function () {
				var c = this.pageNum;
				var ltp = this.pages - 2;
				if (c <= 3)
					c = 3;
				if (c >= ltp)
					c = ltp;

				return [c - 2, c - 1, c, c + 1, c + 2]
					.filter(function (it) {
						return it >= 1 && it <= this.pages;
					}.bind(this));
			},
			checkedIds: function () {
				return this.list.filter(function (it) {
					return it.checked;
				}).map(function (it) {
					return it.id;
				});
			}
		}
	};

	['el', 'created', 'i18n'].forEach(function (it) {
		if (it in a) o[it] = a[it];
	});
	['data', 'methods', 'computed', 'watch'].forEach(function (it) {
		if (it in a) for (var i in a[it]) o[it][i] = a[it][i];
	});

	return o;
}
